<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Appendix II &middot; Crafting Interpreters</title>

<!-- Tell mobile browsers we're optimized for them and they don't need to crop
     the viewport. -->
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="style.css" />

<!-- Oh, God, Source Code Pro is so beautiful it makes me want to cry. -->
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400|Source+Sans+Pro:300,400,600' rel='stylesheet' type='text/css'>

<link rel="icon" type="image/png" href="image/favicon.png" />
<script src="jquery-3.4.1.min.js"></script>
<script src="script.js"></script>

<!-- Google analytics -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-42804721-2', 'auto');
  ga('send', 'pageview');
</script>

</head>
<body id="top">

<!-- <div class="scrim"></div> -->
<nav class="wide">
  <a href="/"><img src="image/logotype.png" title="Crafting Interpreters"></a>
  <div class="contents">
<h3><a href="#top">Appendix II<small>A2</small></a></h3>

<ul>
    <li><a href="#expressions"><small>A2.1</small> Expressions</a></li>
    <li><a href="#statements"><small>A2.2</small> Statements</a></li>
</ul>


<div class="prev-next">
    <a href="appendix-i.html" title="Appendix I" class="left">&larr;&nbsp;Previous</a>
    <a href="backmatter.html" title="Backmatter">&uarr;&nbsp;Up</a>
</div>  </div>
</nav>

<nav class="narrow">
<a href="/"><img src="image/logotype.png" title="Crafting Interpreters"></a>
<a href="appendix-i.html" title="Appendix I" class="prev">←</a>
</nav>

<div class="page">
<div class="nav-wrapper">
<nav class="floating">
  <a href="/"><img src="image/logotype.png" title="Crafting Interpreters"></a>
  <div class="expandable">
<h3><a href="#top">Appendix II<small>A2</small></a></h3>

<ul>
    <li><a href="#expressions"><small>A2.1</small> Expressions</a></li>
    <li><a href="#statements"><small>A2.2</small> Statements</a></li>
</ul>


<div class="prev-next">
    <a href="appendix-i.html" title="Appendix I" class="left">&larr;&nbsp;Previous</a>
    <a href="backmatter.html" title="Backmatter">&uarr;&nbsp;Up</a>
</div>  </div>
  <a id="expand-nav">≡</a>
</nav>
</div>

<article class="chapter">

  <div class="number">A2</div>
  <h1>Appendix II</h1>

<p>For your edification, here is the code produced by <a href="representing-code.html#metaprogramming-the-trees">the little script
we built</a> to automate generating the syntax tree classes for jlox.</p>
<h2><a href="#expressions" id="expressions"><small>A2&#8202;.&#8202;1</small>Expressions</a></h2>
<p>Expressions are the first syntax tree nodes we see, introduced in &ldquo;<a href="representing-code.html">Representing
Code</a>&rdquo;. The main Expr class defines the visitor
interface used to dispatch against the specific expression types, and contains
the other expression subclasses as nested classes.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
create new file</div>
<pre><span class="k">package</span> <span class="i">com.craftinginterpreters.lox</span>;

<span class="k">import</span> <span class="i">java.util.List</span>;

<span class="k">abstract</span> <span class="k">class</span> <span class="t">Expr</span> {
  <span class="k">interface</span> <span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; {
    <span class="t">R</span> <span class="i">visitAssignExpr</span>(<span class="t">Assign</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitBinaryExpr</span>(<span class="t">Binary</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitCallExpr</span>(<span class="t">Call</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitGetExpr</span>(<span class="t">Get</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitGroupingExpr</span>(<span class="t">Grouping</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitLiteralExpr</span>(<span class="t">Literal</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitLogicalExpr</span>(<span class="t">Logical</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitSetExpr</span>(<span class="t">Set</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitSuperExpr</span>(<span class="t">Super</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitThisExpr</span>(<span class="t">This</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitUnaryExpr</span>(<span class="t">Unary</span> <span class="i">expr</span>);
    <span class="t">R</span> <span class="i">visitVariableExpr</span>(<span class="t">Variable</span> <span class="i">expr</span>);
  }

  <span class="c">// Nested Expr classes here...</span>

  <span class="k">abstract</span> &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>);
}
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, create new file</div>

<h3><a href="#assign-expression" id="assign-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;1</small>Assign expression</a></h3>
<p>Variable assignment is introduced in &ldquo;<a href="statements-and-state.html#assignment">Statements and
State</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Assign</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Assign</span>(<span class="t">Token</span> <span class="i">name</span>, <span class="t">Expr</span> <span class="i">value</span>) {
      <span class="k">this</span>.<span class="i">name</span> = <span class="i">name</span>;
      <span class="k">this</span>.<span class="i">value</span> = <span class="i">value</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitAssignExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">name</span>;
    <span class="k">final</span> <span class="t">Expr</span> <span class="i">value</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#binary-expression" id="binary-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;2</small>Binary expression</a></h3>
<p>Binary operators are introduced in &ldquo;<a href="representing-code.html">Representing
Code</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Binary</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Binary</span>(<span class="t">Expr</span> <span class="i">left</span>, <span class="t">Token</span> <span class="i">operator</span>, <span class="t">Expr</span> <span class="i">right</span>) {
      <span class="k">this</span>.<span class="i">left</span> = <span class="i">left</span>;
      <span class="k">this</span>.<span class="i">operator</span> = <span class="i">operator</span>;
      <span class="k">this</span>.<span class="i">right</span> = <span class="i">right</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitBinaryExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">left</span>;
    <span class="k">final</span> <span class="t">Token</span> <span class="i">operator</span>;
    <span class="k">final</span> <span class="t">Expr</span> <span class="i">right</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#call-expression" id="call-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;3</small>Call expression</a></h3>
<p>Function call expressions are introduced in
&ldquo;<a href="functions.html#function-calls">Functions</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Call</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Call</span>(<span class="t">Expr</span> <span class="i">callee</span>, <span class="t">Token</span> <span class="i">paren</span>, <span class="t">List</span>&lt;<span class="t">Expr</span>&gt; <span class="i">arguments</span>) {
      <span class="k">this</span>.<span class="i">callee</span> = <span class="i">callee</span>;
      <span class="k">this</span>.<span class="i">paren</span> = <span class="i">paren</span>;
      <span class="k">this</span>.<span class="i">arguments</span> = <span class="i">arguments</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitCallExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">callee</span>;
    <span class="k">final</span> <span class="t">Token</span> <span class="i">paren</span>;
    <span class="k">final</span> <span class="t">List</span>&lt;<span class="t">Expr</span>&gt; <span class="i">arguments</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#get-expression" id="get-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;4</small>Get expression</a></h3>
<p>Property access, or &ldquo;get&rdquo; expressions are introduced in
&ldquo;<a href="classes.html#properties-on-instances">Classes</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Get</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Get</span>(<span class="t">Expr</span> <span class="i">object</span>, <span class="t">Token</span> <span class="i">name</span>) {
      <span class="k">this</span>.<span class="i">object</span> = <span class="i">object</span>;
      <span class="k">this</span>.<span class="i">name</span> = <span class="i">name</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitGetExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">object</span>;
    <span class="k">final</span> <span class="t">Token</span> <span class="i">name</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#grouping-expression" id="grouping-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;5</small>Grouping expression</a></h3>
<p>Using parentheses to group expressions is introduced in &ldquo;<a href="representing-code.html">Representing
Code</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Grouping</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Grouping</span>(<span class="t">Expr</span> <span class="i">expression</span>) {
      <span class="k">this</span>.<span class="i">expression</span> = <span class="i">expression</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitGroupingExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">expression</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#literal-expression" id="literal-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;6</small>Literal expression</a></h3>
<p>Literal value expressions are introduced in &ldquo;<a href="representing-code.html">Representing
Code</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Literal</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Literal</span>(<span class="t">Object</span> <span class="i">value</span>) {
      <span class="k">this</span>.<span class="i">value</span> = <span class="i">value</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitLiteralExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Object</span> <span class="i">value</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#logical-expression" id="logical-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;7</small>Logical expression</a></h3>
<p>The logical <code>and</code> and <code>or</code> operators are introduced in &ldquo;<a href="control-flow.html#logical-operators">Control
Flow</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Logical</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Logical</span>(<span class="t">Expr</span> <span class="i">left</span>, <span class="t">Token</span> <span class="i">operator</span>, <span class="t">Expr</span> <span class="i">right</span>) {
      <span class="k">this</span>.<span class="i">left</span> = <span class="i">left</span>;
      <span class="k">this</span>.<span class="i">operator</span> = <span class="i">operator</span>;
      <span class="k">this</span>.<span class="i">right</span> = <span class="i">right</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitLogicalExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">left</span>;
    <span class="k">final</span> <span class="t">Token</span> <span class="i">operator</span>;
    <span class="k">final</span> <span class="t">Expr</span> <span class="i">right</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#set-expression" id="set-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;8</small>Set expression</a></h3>
<p>Property assignment, or &ldquo;set&rdquo; expressions are introduced in
&ldquo;<a href="classes.html#properties-on-instances">Classes</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Set</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Set</span>(<span class="t">Expr</span> <span class="i">object</span>, <span class="t">Token</span> <span class="i">name</span>, <span class="t">Expr</span> <span class="i">value</span>) {
      <span class="k">this</span>.<span class="i">object</span> = <span class="i">object</span>;
      <span class="k">this</span>.<span class="i">name</span> = <span class="i">name</span>;
      <span class="k">this</span>.<span class="i">value</span> = <span class="i">value</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitSetExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">object</span>;
    <span class="k">final</span> <span class="t">Token</span> <span class="i">name</span>;
    <span class="k">final</span> <span class="t">Expr</span> <span class="i">value</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#super-expression" id="super-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;9</small>Super expression</a></h3>
<p>The <code>super</code> expression is introduced in
&ldquo;<a href="inheritance.html#calling-superclass-methods">Inheritance</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Super</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Super</span>(<span class="t">Token</span> <span class="i">keyword</span>, <span class="t">Token</span> <span class="i">method</span>) {
      <span class="k">this</span>.<span class="i">keyword</span> = <span class="i">keyword</span>;
      <span class="k">this</span>.<span class="i">method</span> = <span class="i">method</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitSuperExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">keyword</span>;
    <span class="k">final</span> <span class="t">Token</span> <span class="i">method</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#this-expression" id="this-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;10</small>This expression</a></h3>
<p>The <code>this</code> expression is introduced in &ldquo;<a href="classes.html#this">Classes</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">This</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">This</span>(<span class="t">Token</span> <span class="i">keyword</span>) {
      <span class="k">this</span>.<span class="i">keyword</span> = <span class="i">keyword</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitThisExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">keyword</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#unary-expression" id="unary-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;11</small>Unary expression</a></h3>
<p>Unary operators are introduced in &ldquo;<a href="representing-code.html">Representing Code</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Unary</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Unary</span>(<span class="t">Token</span> <span class="i">operator</span>, <span class="t">Expr</span> <span class="i">right</span>) {
      <span class="k">this</span>.<span class="i">operator</span> = <span class="i">operator</span>;
      <span class="k">this</span>.<span class="i">right</span> = <span class="i">right</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitUnaryExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">operator</span>;
    <span class="k">final</span> <span class="t">Expr</span> <span class="i">right</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h3><a href="#variable-expression" id="variable-expression"><small>A2&#8202;.&#8202;1&#8202;.&#8202;12</small>Variable expression</a></h3>
<p>Variable access expressions are introduced in &ldquo;<a href="statements-and-state.html#variable-syntax">Statements and
State</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Expr.java</em><br>
nest inside class <em>Expr</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Variable</span> <span class="k">extends</span> <span class="t">Expr</span> {
    <span class="t">Variable</span>(<span class="t">Token</span> <span class="i">name</span>) {
      <span class="k">this</span>.<span class="i">name</span> = <span class="i">name</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitVariableExpr</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">name</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Expr.java</em>, nest inside class <em>Expr</em></div>

<h2><a href="#statements" id="statements"><small>A2&#8202;.&#8202;2</small>Statements</a></h2>
<p>Statements form a second hierarchy of syntax tree nodes independent of
expressions. We add the first couple of them in &ldquo;<a href="statements-and-state.html">Statements and
State</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
create new file</div>
<pre><span class="k">package</span> <span class="i">com.craftinginterpreters.lox</span>;

<span class="k">import</span> <span class="i">java.util.List</span>;

<span class="k">abstract</span> <span class="k">class</span> <span class="t">Stmt</span> {
  <span class="k">interface</span> <span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; {
    <span class="t">R</span> <span class="i">visitBlockStmt</span>(<span class="t">Block</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitClassStmt</span>(<span class="t">Class</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitExpressionStmt</span>(<span class="t">Expression</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitFunctionStmt</span>(<span class="t">Function</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitIfStmt</span>(<span class="t">If</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitPrintStmt</span>(<span class="t">Print</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitReturnStmt</span>(<span class="t">Return</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitVarStmt</span>(<span class="t">Var</span> <span class="i">stmt</span>);
    <span class="t">R</span> <span class="i">visitWhileStmt</span>(<span class="t">While</span> <span class="i">stmt</span>);
  }

  <span class="c">// Nested Stmt classes here...</span>

  <span class="k">abstract</span> &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>);
}
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, create new file</div>

<h3><a href="#block-statement" id="block-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;1</small>Block statement</a></h3>
<p>The curly-braced block statement that defines a local scope is introduced in
&ldquo;<a href="statements-and-state.html#block-syntax-and-semantics">Statements and State</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Block</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">Block</span>(<span class="t">List</span>&lt;<span class="t">Stmt</span>&gt; <span class="i">statements</span>) {
      <span class="k">this</span>.<span class="i">statements</span> = <span class="i">statements</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitBlockStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">List</span>&lt;<span class="t">Stmt</span>&gt; <span class="i">statements</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#class-statement" id="class-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;2</small>Class statement</a></h3>
<p>Class declarations are introduced in, unsurprisingly,
&ldquo;<a href="classes.html#class-declarations">Classes</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Class</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">Class</span>(<span class="t">Token</span> <span class="i">name</span>,
          <span class="t">Expr</span>.<span class="t">Variable</span> <span class="i">superclass</span>,
          <span class="t">List</span>&lt;<span class="t">Stmt</span>.<span class="t">Function</span>&gt; <span class="i">methods</span>) {
      <span class="k">this</span>.<span class="i">name</span> = <span class="i">name</span>;
      <span class="k">this</span>.<span class="i">superclass</span> = <span class="i">superclass</span>;
      <span class="k">this</span>.<span class="i">methods</span> = <span class="i">methods</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitClassStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">name</span>;
    <span class="k">final</span> <span class="t">Expr</span>.<span class="t">Variable</span> <span class="i">superclass</span>;
    <span class="k">final</span> <span class="t">List</span>&lt;<span class="t">Stmt</span>.<span class="t">Function</span>&gt; <span class="i">methods</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#expression-statement" id="expression-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;3</small>Expression statement</a></h3>
<p>The expression statement is introduced in &ldquo;<a href="statements-and-state.html#statements">Statements and
State</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Expression</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">Expression</span>(<span class="t">Expr</span> <span class="i">expression</span>) {
      <span class="k">this</span>.<span class="i">expression</span> = <span class="i">expression</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitExpressionStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">expression</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#function-statement" id="function-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;4</small>Function statement</a></h3>
<p>Function declarations are introduced in, you guessed it,
&ldquo;<a href="functions.html#function-declarations">Functions</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Function</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">Function</span>(<span class="t">Token</span> <span class="i">name</span>, <span class="t">List</span>&lt;<span class="t">Token</span>&gt; <span class="i">params</span>, <span class="t">List</span>&lt;<span class="t">Stmt</span>&gt; <span class="i">body</span>) {
      <span class="k">this</span>.<span class="i">name</span> = <span class="i">name</span>;
      <span class="k">this</span>.<span class="i">params</span> = <span class="i">params</span>;
      <span class="k">this</span>.<span class="i">body</span> = <span class="i">body</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitFunctionStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">name</span>;
    <span class="k">final</span> <span class="t">List</span>&lt;<span class="t">Token</span>&gt; <span class="i">params</span>;
    <span class="k">final</span> <span class="t">List</span>&lt;<span class="t">Stmt</span>&gt; <span class="i">body</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#if-statement" id="if-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;5</small>If statement</a></h3>
<p>The <code>if</code> statement is introduced in &ldquo;<a href="control-flow.html#conditional-execution">Control
Flow</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">If</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">If</span>(<span class="t">Expr</span> <span class="i">condition</span>, <span class="t">Stmt</span> <span class="i">thenBranch</span>, <span class="t">Stmt</span> <span class="i">elseBranch</span>) {
      <span class="k">this</span>.<span class="i">condition</span> = <span class="i">condition</span>;
      <span class="k">this</span>.<span class="i">thenBranch</span> = <span class="i">thenBranch</span>;
      <span class="k">this</span>.<span class="i">elseBranch</span> = <span class="i">elseBranch</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitIfStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">condition</span>;
    <span class="k">final</span> <span class="t">Stmt</span> <span class="i">thenBranch</span>;
    <span class="k">final</span> <span class="t">Stmt</span> <span class="i">elseBranch</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#print-statement" id="print-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;6</small>Print statement</a></h3>
<p>The <code>print</code> statement is introduced in &ldquo;<a href="statements-and-state.html#statements">Statements and
State</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Print</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">Print</span>(<span class="t">Expr</span> <span class="i">expression</span>) {
      <span class="k">this</span>.<span class="i">expression</span> = <span class="i">expression</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitPrintStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">expression</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#return-statement" id="return-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;7</small>Return statement</a></h3>
<p>You need a function to return from, so <code>return</code> statements are introduced in
&ldquo;<a href="functions.html#return-statements">Functions</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Return</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">Return</span>(<span class="t">Token</span> <span class="i">keyword</span>, <span class="t">Expr</span> <span class="i">value</span>) {
      <span class="k">this</span>.<span class="i">keyword</span> = <span class="i">keyword</span>;
      <span class="k">this</span>.<span class="i">value</span> = <span class="i">value</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitReturnStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">keyword</span>;
    <span class="k">final</span> <span class="t">Expr</span> <span class="i">value</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#variable-statement" id="variable-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;8</small>Variable statement</a></h3>
<p>Variable declarations are introduced in &ldquo;<a href="statements-and-state.html#variable-syntax">Statements and
State</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">Var</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">Var</span>(<span class="t">Token</span> <span class="i">name</span>, <span class="t">Expr</span> <span class="i">initializer</span>) {
      <span class="k">this</span>.<span class="i">name</span> = <span class="i">name</span>;
      <span class="k">this</span>.<span class="i">initializer</span> = <span class="i">initializer</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitVarStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Token</span> <span class="i">name</span>;
    <span class="k">final</span> <span class="t">Expr</span> <span class="i">initializer</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>

<h3><a href="#while-statement" id="while-statement"><small>A2&#8202;.&#8202;2&#8202;.&#8202;9</small>While statement</a></h3>
<p>The <code>while</code> statement is introduced in &ldquo;<a href="control-flow.html#while-loops">Control
Flow</a>&rdquo;.</p>
<div class="codehilite"><div class="source-file"><em>lox/Stmt.java</em><br>
nest inside class <em>Stmt</em></div>
<pre>  <span class="k">static</span> <span class="k">class</span> <span class="t">While</span> <span class="k">extends</span> <span class="t">Stmt</span> {
    <span class="t">While</span>(<span class="t">Expr</span> <span class="i">condition</span>, <span class="t">Stmt</span> <span class="i">body</span>) {
      <span class="k">this</span>.<span class="i">condition</span> = <span class="i">condition</span>;
      <span class="k">this</span>.<span class="i">body</span> = <span class="i">body</span>;
    }

    <span class="a">@Override</span>
    &lt;<span class="t">R</span>&gt; <span class="t">R</span> <span class="i">accept</span>(<span class="t">Visitor</span>&lt;<span class="t">R</span>&gt; <span class="i">visitor</span>) {
      <span class="k">return</span> <span class="i">visitor</span>.<span class="i">visitWhileStmt</span>(<span class="k">this</span>);
    }

    <span class="k">final</span> <span class="t">Expr</span> <span class="i">condition</span>;
    <span class="k">final</span> <span class="t">Stmt</span> <span class="i">body</span>;
  }
</pre></div>
<div class="source-file-narrow"><em>lox/Stmt.java</em>, nest inside class <em>Stmt</em></div>


<footer>
Handcrafted by Robert Nystrom&ensp;&mdash;&ensp;<a href="https://github.com/munificent/craftinginterpreters/blob/master/LICENSE" target="_blank">&copy; 2015&hairsp;&ndash;&hairsp;2021</a>
</footer>
</article>

</div>
</body>
</html>
